package com.itheima.controller;


import com.itheima.anno.Log;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/depts")
public class DeptController {

    @Autowired
    private DeptService deptService;


    /**
     * 查询全部部门数据
     */
    @GetMapping
    public Result list(){
        System.out.println("查询全部部门数据");
        List<Dept> deptList =  deptService.findAll();
        return Result.success(deptList);
    }

    /**
     * 根据id删除部门
     */
    @Log
    @DeleteMapping
    public Result delete(Integer id){
        System.out.println("删除部门:" + id);
        deptService.deleteById(id);
        return Result.success();
    }

    /**
     * 新增部门
     */
    @Log
    @PostMapping
    public Result add(@RequestBody Dept dept){
        System.out.println("新增部门:"+dept);
        deptService.add(dept);
        return Result.success();
    }

    /**
     * 根据id查询部门（查询回显）
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        System.out.println("根据id查询部门：" + id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

    /**
     * 根据id查询部门（查询回显）
     */
    @Log
    @PutMapping
    public Result update(@RequestBody Dept dept){
        System.out.println("根据id修改部门：" + dept);
        deptService.update(dept);
        return Result.success();
    }


}
